
//
//  PublishSelectorView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/1/29.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class PublishSelectorView: UIView {
    private let scale: CGFloat = 1.2
    private let selfHeight: CGFloat = 80
    private let titles: [String]
    private let images: [String]
    private let LeftAndRightMargin: CGFloat = 17
    private let ItemMargin: CGFloat = 21.5
    private let TopMargin: CGFloat = 6.5
    private let showView: BaseCircleView
    private var tapClosure: ((NSInteger) -> Void)?
    init(_ titles:[String], _ images:[String]) {
        self.titles = titles
        self.images = images
        showView = BaseCircleView()
        super.init(frame: .zero)
        self.alpha = 0.2
        setUI()
    }
    
    private func setUI() {
        
        for (index, item) in titles.enumerated() {
            
            let button = UIButton(type: .custom)
            let image = UIImage(named: images[index])
            guard let finalImage = image else {
                return assert(true, "图片名有误")
            }
            
            let itemSize = scaleImage(image: finalImage, scale: scale).size.width
            button.tag = index
            button.setTitleColor(UIColor.white, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 10)
            button.frame = CGRect(x: LeftAndRightMargin + CGFloat(index) * itemSize + ItemMargin * CGFloat(index), y: 0, width: itemSize, height: selfHeight)
            
            button.setImage(UIImage(named: images[index])!, for: .normal)
            button.setTitle(item, for: .normal)
//            button.set(image: scaleImage(image: UIImage(named: images[index])!, scale: scale), title: item, titlePosition: .bottom, additionalSpacing: -5, imageTopMargin: -10, state: .normal)
            button.setImagePositionWith(SSImagePositionType.top, spacing: 5)
            button .addTarget(self, action: #selector(tapButton(_ :)), for: .touchUpInside)
            showView.addSubview(button)
            
            if index == 0 {
                let yPoint: CGFloat  = ScreenHeight - Ipx(false).bottomMargin - BarHeight - 15 - selfHeight
                let width1: CGFloat = CGFloat(titles.count) * itemSize + LeftAndRightMargin * 2
                let width = width1 + CGFloat(titles.count - 1) * ItemMargin
                showView.frame = CGRect(x: showView.frame.origin.x, y: yPoint, width: width, height: selfHeight)
                showView.center = CGPoint(x: ScreenWidth / 2, y:showView.centerY)
                showView.backgroundColor = UIColor(hex: 0x22631)
                self.addSubview(showView)
            }
        }
    }
    
    @objc func tapButton(_ btn: UIButton) {
        hide()
        self.tapClosure?(btn.tag)
    }
    
    func show(_ tapClosure: @escaping (NSInteger) -> Void) {
        self.tapClosure = tapClosure
        let window = UIApplication.shared.keyWindow
        self.backgroundColor = UIColor.white
        self.frame = window!.bounds
        self.backgroundColor = ONColor.maskBGColor
        window?.addSubview(self)
        
        UIView.animate(withDuration: TimeInterval(anTIME)) {
            self.alpha = 1;
        }
    }
    
    func hide() {
        UIView.animate(withDuration: TimeInterval(anTIME), animations: {
            self.alpha = 0.2
        }) { (aha) in
            self.removeFromSuperview()
        } 
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touchView = touches.first?.view {
            if touchView.isEqual(self) {
                hide()
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func scaleImage(image: UIImage, scale: CGFloat) -> UIImage {
        
        let size = CGSize(width: image.size.width * scale, height: image.size.height * scale)
        UIGraphicsBeginImageContext(size)
        image.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: size))
        let scaleImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return scaleImage!
    }
}
